package com.team.arms.yuding.controller;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.yuding.domain.VoluntaryInformation;
import com.team.arms.yuding.domain.dto.VoluntaryInformationDto;
import com.team.arms.yuding.domain.vo.VoluntaryInformationVo;
import com.team.arms.yuding.mapper.VoluntaryInformationMapper;
import com.team.arms.yuding.service.VoluntaryInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjh
 * @since 2022-04-27
 */
@Api( tags = "志愿信息")
@RestController
@RequestMapping("/reservesoldiers/information")
public class VoluntaryInformationController extends BaseController {

    @Autowired
    private VoluntaryInformationService voluntaryInformationService;
    @Autowired
    private VoluntaryInformationMapper voluntaryInformationMapper;



    /**
     * 分页查询志愿填表信息
     * @return
     */
    @ApiOperation("查询志愿填报信息")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<VoluntaryInformationVo> list = voluntaryInformationService.selectList();
        PageInfo<VoluntaryInformationVo> pageInfo = new PageInfo<VoluntaryInformationVo>(list);
        PageVO<VoluntaryInformationVo> voluntaryInformationPageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, voluntaryInformationPageVO);
        voluntaryInformationPageVO.setResult(pageInfo.getList());
        voluntaryInformationPageVO.setPage(pageNum);
        return AjaxResult.success(voluntaryInformationPageVO);
    }

    @ApiOperation("根据id查询志愿填报信息")
    @GetMapping("/selectById")
    public AjaxResult selectById(@RequestParam(value = "volunteer_info_id")String id){
        return AjaxResult.success(voluntaryInformationService.selectById(id));
    }


    @ApiOperation("根据条件查询志愿填报信息")
    @PostMapping("/selectvolunteer")
    public TableDataInfo selectVolunteer(
            @RequestBody(required = false) VoluntaryInformationDto voluntaryInformationDto){

        startPage();

        List<VoluntaryInformationVo> list = voluntaryInformationService.selectVoluntaryInformation(voluntaryInformationDto);

        return getDataTable(list);
    }

    /*
    @ApiOperation("根据对应字段查询志愿填报信息")
    @GetMapping("/selectByFile")
    public AjaxResult selectByName( @RequestParam(value = "查询的字段") String field ,@RequestParam(value = "查询的内容")String content){

        if (field.equals("姓名")) {
            return AjaxResult.success(voluntaryInformationMapper.selectByname(content));
        }
        if (field.equals("批次编号")) {
            return AjaxResult.success(voluntaryInformationMapper.selectByBatchId(content));
        }
        if (field.equals("志愿1")) {
            return AjaxResult.success(voluntaryInformationMapper.selectByVolunteerOne(content));
        }
        if (field.equals("志愿2")) {
            return AjaxResult.success(voluntaryInformationMapper.selectByVolunteerTwo(content));
        }
        if (field.equals("学历")) {
            return AjaxResult.success(voluntaryInformationMapper.selectByCultureProgram(content));
        }
        return AjaxResult.error();
    }*/

    @ApiOperation("根据对应字段查询志愿填报信息")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        return AjaxResult.success(voluntaryInformationMapper.selectByFiled(field,content));
    }

    /**
     * 修改志愿填报信息
     * @param voluntaryInformationDto
     * @return
     */
    @ApiOperation("修改志愿填报信息")
    @PutMapping("/update")
    public AjaxResult updateInformation(
            @RequestParam(value = "volunteer_info_id")String id,
            @RequestBody VoluntaryInformationDto voluntaryInformationDto){
        return toAjax(voluntaryInformationMapper.updateAll(voluntaryInformationDto,id));
    }


    /**
     * 新增志愿填报信息
     */
    @ApiOperation("新增志愿填报信息")
    @PostMapping("/install")
    public AjaxResult installInformation(@RequestBody VoluntaryInformation voluntaryInformation){
        return toAjax(voluntaryInformationService.insertAll(voluntaryInformation));
    }

    /**
     * 批量删除志愿填报信息
     * @param ids
     * @return
     */
    @ApiOperation("批量删除志愿填报信息")
    @DeleteMapping("/delete")
    public AjaxResult deleteInformation(@RequestBody String[] ids){
        ArrayList<String> list = new ArrayList<String>();
        for(String id : ids){
            list.add(id);
        }
        return toAjax(voluntaryInformationService.removeBatchByIds(list));
    }

    /**
     * 删除志愿填报信息
     * @param id
     * @return
     */
    @ApiOperation("删除志愿填报信息")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam("volunteer_info_id") String id){
        return toAjax(voluntaryInformationService.removeById(id));
    }



    /**
     * 导出志愿填报信息
     */
    @ApiOperation("导出志愿填报信息")
    @Log(title = "志愿填报信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<VoluntaryInformationVo> voluntaryInformationVoList = voluntaryInformationService.selectList();
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), VoluntaryInformation.class).sheet("志愿填报信息")
                    .doWrite(voluntaryInformationVoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

